/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.command.transaction;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.openinsula.vulcano.orm.dao.query.builder.DaoQueryBuilder;
import org.openinsula.vulcano.orm.jpa.entity.SimpleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = { "/test-context-weaver.xml", "classpath*:META-INF/*-context.xml" })
public class TransactionInvokerFacadeTestCase extends AbstractJUnit4SpringContextTests {

	@Autowired
	private DaoCommandTransactionFacade transactionFacade;

	@Before
	public void before() {
		transactionFacade.deleteAll(SimpleEntity.class);
	}

	@Test
	public void testSaveArray() {
		SimpleEntity[] entities = new SimpleEntity[] { new SimpleEntity(), new SimpleEntity(), new SimpleEntity(),
				new SimpleEntity(), new SimpleEntity() };

		assertEquals(5, transactionFacade.save(entities));
	}

	@Test
	public void testSaveCollection() {
		Collection<SimpleEntity> entities = new ArrayList<SimpleEntity>();
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());
		entities.add(new SimpleEntity());

		assertEquals(5, transactionFacade.saveAll(entities));
	}

	@Test
	public void testFindByDaoQuery() {
		transactionFacade.deleteAll(SimpleEntity.class);

		SimpleEntity entity = new SimpleEntity();
		entity.setName("John Doe");
		transactionFacade.save(entity);

		entity = new SimpleEntity();
		entity.setName("Joe Doe");
		transactionFacade.save(entity);

		DaoQuery daoQuery = new DaoQueryBuilder().select().from(SimpleEntity.class).getDaoQuery();

		List<SimpleEntity> result = transactionFacade.<SimpleEntity> find(daoQuery);

		assertEquals(2, result.size());

	}
	//	
	// @Test
	// public void testDeleteCollection() {
	// }
	//	
	// @Test
	// public void testDeleteType() {
	// }

}
